package com.ai.WS;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class AIWS {
	private static final String wsURL = "http://www.fsdigital.com/WsConserve/Conserve.asmx/";

	private static final int HTTP_STATUS_OK = 200;

	private static final String logTag = "LookFMHelper";

	public enum metodos {
		login, Singup, GetCategorys, NovoPoint, GetPoints, GetComentarios, NovoComentario, NovoApoio
	}

	protected static synchronized String login(String... params)
			throws ClientProtocolException, IOException {

		String url = wsURL + "Login";
		Log.d(logTag, "Fetching " + url);
		JSONObject obj = new JSONObject();
		try {
			obj.put("email", params[0]);
			obj.put("senha", params[1]);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return Request(url, obj);
	}

	protected static synchronized String signUP(String... params)
			throws ClientProtocolException, IOException {
		String url = wsURL + "SignUP";

		JSONObject obj = new JSONObject();
		try {
			obj.put("nome", params[0]);
			obj.put("email", params[1]);
			obj.put("senha", params[2]);
			obj.put("sexo", params[3]);

		} catch (JSONException e) {
			Log.d("problema Json", e.getMessage());
		}
		return Request(url, obj);
	}

	protected static synchronized String getCategorys(String... params)
			throws ClientProtocolException, IOException {

		String url = wsURL + "GetCategory";
		Log.d(logTag, "Fetching " + url);
		JSONObject obj = new JSONObject();
		try {
			obj.put("token", params[0]);
		} catch (JSONException e) {
			Log.d("problema Json", e.getMessage());
		}
		return Request(url, obj);
	}

	protected static synchronized String novoPoint(String... params)
			throws ClientProtocolException, IOException {

		String url = wsURL + "PointConserve";
		JSONObject obj = new JSONObject();
		try {
			obj.put("token", params[0]);
			obj.put("latitude", params[1]);
			obj.put("longitude", params[2]);
			obj.put("idCategoria", params[3]);
			obj.put("texto", params[4]);
			obj.put("imagem", params[5]);
			obj.put("video", params[6]);
		} catch (JSONException e) {
			Log.d("mensagem", e.getMessage());
		}
		return Request(url, obj);
	}

	protected static synchronized String novoApoio(String... params)
			throws ClientProtocolException, IOException {

		String url = wsURL + "PointConserveApoiar";
		JSONObject obj = new JSONObject();
		try {
			obj.put("token", params[0]);
			obj.put("apoio", params[1]);
			obj.put("idPointConserve", params[2]);

		} catch (JSONException e) {
			Log.d("mensagem", e.getMessage());
		}
		return Request(url, obj);
	}

	protected static synchronized String novoComentario(String... params)
			throws ClientProtocolException, IOException {

		String url = wsURL + "PointConserveComentario";
		JSONObject obj = new JSONObject();
		try {
			obj.put("token", params[0]);
			obj.put("texto", params[1]);
			obj.put("idPointConserve", params[2]);

		} catch (JSONException e) {
			Log.d("mensagem", e.getMessage());
		}
		return Request(url, obj);
	}

	protected static synchronized String listarPointConserveComentario(
			String... params) throws ClientProtocolException, IOException {

		String url = wsURL + "ListarPointConserveComentario";
		JSONObject obj = new JSONObject();
		try {
			obj.put("token", params[0]);
			obj.put("idPointConserve", params[1]);

		} catch (JSONException e) {
			Log.d("mensagem", e.getMessage());
		}
		return Request(url, obj);
	}

	protected static synchronized String getPoints(String... params)
			throws ClientProtocolException, IOException {

		String url = wsURL + "GetPoints";
		JSONObject obj = new JSONObject();
		try {
			String latitude = "";
			String longitude = "";
			String idCategoria = "";
			String token = "";
			if (params != null && params.length>0) {
				token = params[0];
				if (params.length > 1) {
					latitude = params[1];
					longitude = params[2];
					idCategoria = params[3];
				}
			}

			obj.put("latitude", latitude);
			obj.put("longitude", longitude);
			obj.put("idCategoria", idCategoria);
			obj.put("token", token);
		} catch (JSONException e) {
			Log.d("PROBLEMA jSON", e.getMessage());
		}
		return Request(url, obj);
	}

	public static String Request(String strUrl, JSONObject obj)
			throws ClientProtocolException, IOException {

		// create an http client and a request object.

		URL url = new URL(strUrl);
		HttpURLConnection conexao = (HttpURLConnection) url.openConnection();
		conexao.addRequestProperty("Accept", "application/json");
		conexao.addRequestProperty("Content-type", "application/json");
		conexao.setRequestMethod("POST");
		conexao.setDoInput(true);
		conexao.setDoOutput(true);

		OutputStreamWriter out = new OutputStreamWriter(
				conexao.getOutputStream());
		out.write(obj.toString());
		out.close();
		conexao.connect();
		// HttpPost request = new HttpPost(url);
		// conexao.setHeader("Accept", "application/json");
		// conexao.setHeader("Content-type", "application/json");
		// request.setEntity(new StringEntity(obj.toString(), "UTF-8"));

		if (conexao.getResponseCode() != HTTP_STATUS_OK) {
			// handle error here
			Log.d("PROBLEMA", "response =" + conexao.getResponseMessage() + "");
		}

		InputStream ist = conexao.getInputStream();

		return streamToString(ist);
	}

	private static String streamToString(InputStream is) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));

		StringBuffer jsonBuffer = new StringBuffer();
		String linha;

		while ((linha = reader.readLine()) != null) {
			jsonBuffer.append(linha);
		}

		return jsonBuffer.toString();
	}
}
